package at.bherbst;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class UDPSenderService extends Service implements SensorEventListener{
	private static final String TAG = "UDPSenderService";
	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	private UDPData speedData;
	private UDPData steerData;
	private Thread speedSender;
	private Thread steerSender;
	private static SharedPreferences settings;
	private NotificationManager mNM;
	// Unique Identification Number for the Notification.
    // We use it on Notification start, and to cancel it.
    private int NOTIFICATION = R.string.local_service_started;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onCreate");

		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		mAccelerometer = mSensorManager
				.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		speedData = new UDPData();
		speedData.lock.lock();
		steerData = new UDPData();
		steerData.lock.lock();

		mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // Display a notification about us starting.  We put an icon in the status bar.
        showNotification();
	}

	@Override
	public void onDestroy() {
		Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onDestroy");
        mSensorManager.unregisterListener(this);
		speedSender.interrupt();
		steerSender.interrupt();
		mNM.cancel(NOTIFICATION);
	}

	@Override
	public void onStart(Intent intent, int startid) {
		Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
		settings = this.getSharedPreferences("udpserver",MODE_WORLD_WRITEABLE);
		//get value
		//will return "0" if preference not exists, else return stored value
		String val = settings.getString("serverip", "192.168.43.2");
		speedSender = new Thread(new UDPSenderRunnable(speedData, val));
		speedSender.start();
		steerSender = new Thread(new UDPSenderRunnable(steerData, val));
		steerSender.start();
		mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	/**
     * Show a notification while this service is running.
     */
    private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.local_service_started);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.icon, text,
                System.currentTimeMillis());
        

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, UDPSensorActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                       text, contentIntent);

        // Send the notification.
        // We use a layout id because it is a unique number.  We use it later to cancel.
        mNM.notify(R.string.local_service_started, notification);
    }


	public void onSensorChanged(SensorEvent event) {
		Log.d(TAG, "onSensorChanged");
		speedData.setValue(22+((int)event.values[1]+10),TAG);
		steerData.setValue(62+((int)event.values[0]+10), TAG);
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

}
